package com.xiaoyu.lanling.feature.im.request.flow

import com.xiaoyu.base.app.AppContext
import com.xiaoyu.im.datamodel.MessageStatus
import com.xiaoyu.im.provider.ConversationManager
import com.xiaoyu.im.request.MessageRequest
import com.xiaoyu.im.request.flow.BaseCheckAction
import com.xiaoyu.im.request.flow.BaseCheckAction.CheckSubAction
import com.xiaoyu.lanling.R
import com.xiaoyu.lanling.feature.im.provider.MessageDataProvider

class CheckAction : BaseCheckAction() {

    init {
        checkList.add(CheckSubAction { request: MessageRequest -> checkConversation(request) })
    }

    private fun checkConversation(request: MessageRequest) {
        val chatId = request.chatId
        val conversation = ConversationManager.getInstance().fetchConversation(chatId)
        if (conversation == null) {
            request.status = MessageStatus.NOT_CONVERSATION_FAILED
            return
        }
        if (conversation.isBlack) {
            request.status = MessageStatus.BLACKED_FAILED
            val text = AppContext.getString(R.string.chat_message_admin_black)
            MessageDataProvider.instance.addAdminMessageLocally(chatId, text, "", "")
            return
        }
        if (conversation.isBlacked) {
            request.status = MessageStatus.BLACKED_FAILED
            val text = AppContext.getString(R.string.chat_message_admin_blacked)
            MessageDataProvider.instance.addAdminMessageLocally(chatId, text, "", "")
            return
        }
    }
}